/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

/**
 *
 * @author AlexRodriguez
 */
import Beans.Cantantes;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
public class CantantesDao {
    
    Connection con=null;
    
    public void setConection( Connection con){
        this.con = con;
    }
    
    public boolean existe_Cantante(Cantantes cantantes) throws SQLException
	 {
		ResultSet re=null;
		PreparedStatement ps=null;
		boolean bandera=false;
		try
		{
			ps= con.prepareStatement("Select * from cantantes where id_cantante=?");
                        ps.setString(1, cantantes.getIdcantante());
			re=ps.executeQuery();
			while(re.next())
			{
				bandera=true;
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return bandera;
	}
	
	//Adicionar elementos a la BD

	public void newCantante(Cantantes cantantes) throws SQLException
	{
		if(existe_Cantante(cantantes))
			throw new SQLException("El Cantante Ya Existe");

		PreparedStatement pstmt = null;
		try
		{
			pstmt = con.prepareStatement("INSERT INTO cantantes VALUES(?,?,?)");
			pstmt.setString(1,cantantes.getIdcantante());
			pstmt.setString(2,cantantes.getNombrecantante());
			pstmt.setString(3,cantantes.getSexocantante());
                                           

			pstmt.executeUpdate();
		}
		finally
		{
			if(pstmt != null)
				pstmt.close();
		}
	}
        
        public List listar()throws SQLException{
            	ResultSet re=null;
		PreparedStatement ps=null;
		List lista = new LinkedList();
		try
		{
			ps=(PreparedStatement) con.prepareStatement("Select * from cantantes");
                        re=ps.executeQuery();
			while(re.next())
			{
				lista.add(Cantantes.load(re));
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return lista;
        }
        
        
        public List buscar(int tipo, String parametro)throws SQLException{
            	ResultSet re=null;
		PreparedStatement ps=null;
		List lista = new LinkedList();
                String sql="";
		try
		{
                    if(tipo==1)
                        sql="Select * from cantantes where id_cantante like ?"; 
                     if(tipo==2)
                        sql="Select * from cantantes where nom_cantante like ?"; 
                    
			ps=(PreparedStatement) con.prepareStatement(sql);
                        ps.setString(1,parametro);
                        re=ps.executeQuery();
			while(re.next())
			{
				lista.add(Cantantes.load(re));
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return lista;
        }
        
        public void eliminar(String cod)throws SQLException{
                PreparedStatement pstmt = null;
		try
		{
			pstmt =con.prepareStatement("delete from cantantes where id_cantante=?");
			pstmt.setString(1,cod);
			pstmt.executeUpdate();
		}
		finally
		{
			if(pstmt != null)
				pstmt.close();
		}
        }
        
        public void modificar(Cantantes p)throws SQLException{
            

		PreparedStatement pstmt = null;
		try
		{
			pstmt =con.prepareStatement("Update cantantes set nom_cantante = ?, sexo_cantante = ? where id_cantante = ?");
			pstmt.setString(1,p.getNombrecantante());
			pstmt.setString(2,p.getSexocantante());
                        pstmt.setString(3,p.getIdcantante());
                        pstmt.executeUpdate();
		}
		finally
		{
			if(pstmt != null)
				pstmt.close();
		}
            
        }
        
        public Cantantes obtener(String cod)throws SQLException{
            ResultSet re=null;
		PreparedStatement ps=null;
		Cantantes p = new Cantantes();
		try
		{
			ps=(PreparedStatement) con.prepareStatement("Select * from cantantes where id_cantante=?");
                        re=ps.executeQuery();
			while(re.next())
			{
				p = Cantantes.load(re);
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return p;
        }
        
         public Cantantes getCantante(String id)throws SQLException{
            	ResultSet re=null;
		PreparedStatement ps=null;
		Cantantes p = new Cantantes();
		try
		{
			ps=con.prepareStatement("Select * from cantantes where id_cantante=?");
                        ps.setString(1, id);
                        re=ps.executeQuery();
			if(re.next())
			
				p = Cantantes.load(re);
			
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return p;
        }
            public static Cantantes load(ResultSet filas) throws Exception{

       try {

            Cantantes e = new Cantantes();
            e.setIdcantante(filas.getString("cedula"));
            e.setNombrecantante(filas.getString("nombre"));
            e.setSexocantante(filas.getString("apellido"));
            
            return e;
        } catch (SQLException ex) {
            throw new Exception("Error en la Consulta del Cantante:\n" + ex.getMessage());
        }

    }
    
}
